/*获取当前服务器时间对象
* function getYear--获取年
* function getMonth--获取月
* function getDay--获取日
* function getDateStr--获取时间，param type--时间类型【month，day，hour,min,second】，精确到年月日时分秒
* function getHowLongAgo--获取多长时间之前，param pastTime--事件发生的时间(多去的时间)，nowTime--目前的时间*/
class GetFormatDate {
  constructor(timeStamp) {
    this.timeStamp = timeStamp;
    const date = new Date(this.timeStamp);
    this.seperator1 = '-';
    this.year = date.getFullYear();
    this.month = date.getMonth() + 1;
    this.strDate = date.getDate();
    this.h = date.getHours(); //获取当前小时数(0-23)
    this.m = date.getMinutes(); //获取当前分钟数(0-59)
    this.s = date.getSeconds();
    this.day = date.getDay();
  }
  conver(s) {
    return s < 10 ? '0' + s : s;
  }
  getYear() {
    return this.year
  }
  getMonth() {
    return this.conver(this.month)
  }
  getDay() {
    return this.conver(this.strDate)
  }
  getDateStr(timeType) {
    let currentdate = '';
    if (timeType) {
      switch (timeType) {
        case 'month':
          currentdate = this.year + this.seperator1 + this.conver(this.month)
          break
        case 'day':
          currentdate = this.year + this.seperator1 + this.conver(this.month) + this.seperator1 + this.conver(this.strDate)
          break
        case 'hour':
          currentdate = this.year + this.seperator1 + this.conver(this.month) + this.seperator1 + this.conver(this.strDate) + ' ' + this.conver(this.h)
          break
        case 'min':
          currentdate = this.year + this.seperator1 + this.conver(this.month) + this.seperator1 + this.conver(this.strDate) + ' ' + this.conver(this.h) + ':' + this.conver(this.m)
          break
        case 'second':
          currentdate = this.year + this.seperator1 + this.conver(this.month) + this.seperator1 + this.conver(this.strDate) + ' ' + this.conver(this.h) + ':' + this.conver(this.m) + ':' + this.conver(this.s)
          break
        case 'strSecond':
          currentdate = this.year + this.conver(this.month) + this.conver(this.strDate) + this.conver(this.h) + this.conver(this.m) + this.conver(this.s)
          break
        default:
          currentdate = this.year + this.seperator1 + this.conver(this.month) + this.seperator1 + this.conver(this.strDate) + ' ' + this.conver(this.h) + ':' + this.conver(this.m) + ':' + this.conver(this.s)
          break
      }
      return currentdate
    }
  }
  getDateWeek() {
    var today = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
    var week = today[this.day];
    return week;
  }
  getHowLongAgo(pastTime, nowTime) {
    const pastTimeStr = new Date(pastTime).getTime()
    const nowTimeStr = new Date(nowTime).getTime()
    const timeDifference = new Date(nowTimeStr - pastTimeStr)
    const seconedDifference = parseInt(timeDifference / 1000)
    const minuteDifference = parseInt(timeDifference / 1000 / 60)
    const hourDifference = parseInt(timeDifference / 1000 / 60 / 60)
    const dayDifference = parseInt(timeDifference / 1000 / 60 / 60 / 24)
    const monthDifference = parseInt(timeDifference / 1000 / 60 / 60 / 24 / 30)
    const yearDifference = parseInt(timeDifference / 1000 / 60 / 60 / 24 / 30 / 12)
    if (seconedDifference < 60) {
      return seconedDifference + '秒前'
    } else if (seconedDifference > 60 && minuteDifference < 60) {
      return minuteDifference + '分钟前'
    } else if (minuteDifference > 60 && hourDifference < 24) {
      return hourDifference + '小时前'
    } else if (hourDifference > 24 && dayDifference < 30) {
      return dayDifference + '天前'
    } else if (dayDifference > 30 && monthDifference < 12) {
      return monthDifference + '个月前'
    } else if (monthDifference > 24) {
      return yearDifference + '年前'
    }
  }
}
export const getFormatDate = (timeStr) => {
  return new GetFormatDate(timeStr)
}
